#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File   ：GetCSProduct_New -> cs_api
@IDE    ：PyCharm
@Author ：Json
@Date   ：2021-03-30 18:37
@Desc   ：
=================================================='''
import json

from requests import request, HTTPError

PAM_DEFAULT_TIMEOUT = 15


class Client:
    URI = "/"

    def __init__(self,
                 cs_user,  # 不指定默认值，
                 description=None, ):
        self.cs_user = cs_user
        self.domain = 'https://api.cameronsino.com/b2d/api'
        self.description = description
        self.test_request = False

    def build_url(self):
        pass

    def make_request(self, method, description, data, timeout=PAM_DEFAULT_TIMEOUT, is_save=False):
        url = "{domain}{api_type}{description}".format(
            domain=self.domain,
            api_type=self.URI,
            description=description
        )
        headers = {
            'Accept': 'application/json, text/plain, */*',
            'Content-Type': 'application/json;charset=UTF-8',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36',
            'Authorization': 'Bearer ' + self.cs_user.token}

        if self.test_request:
            print('此模式为测试模式，并未真正请求')
            print('url:' + url)
            print('headers:' + str(headers))
            print('data:' + str(data) + '\n')
            return None
        try:
            response = request(
                method,
                url,
                data=json.dumps(data),
                # data=data,
                headers=headers,
                timeout=timeout,
            )
            response.raise_for_status()
        except HTTPError as exc:
            print(exc)
            print('网络错误')
            return None
        return response


if __name__ == '__main__':
    pass
